import 'package:easy_refresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:sainbainu/common/apis/content_api.dart';
import 'package:sainbainu/common/enums/resource_type.dart';
import 'package:sainbainu/common/models/request/pager_request.dart';
import 'package:sainbainu/common/models/request/resource_request.dart';
import 'package:sainbainu/common/repositories/content_repository.dart';
import 'package:sainbainu/common/services/user.dart';
import 'package:sainbainu/common/utils/loading.dart';

import 'index.dart';

class MyCollectController extends GetxController {
  MyCollectController();

  final state = MyCollectState();

  Worker? worker;

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    state.refreshController = EasyRefreshController(
      controlFinishRefresh: false,
      controlFinishLoad: true,
    );
    state.pagerRequest = PagerRequest();
    state.contentRepository = ContentRepository(ContentApi());

    getList();

    worker = ever(UserService.to.isLogin, (isLogin) {
      if (isLogin) {
        getList();
      }
    });
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
    state.refreshController.dispose();
    worker?.dispose();
  }

  /// 页面显示
  void onPageShow() {
    if (state.needRefresh) {
      state.needRefresh = false;
      getList();
    }
  }

  /// 获取内容列表
  Future getList({bool refresh = true}) async {
    if (refresh) {
      state.pagerRequest.pageNum = 1;
    }
    var res = await state.contentRepository.collectionList(
      state.pagerRequest,
    );
    if (refresh) {
      state.contentList.rows = res.rows;
    } else {
      state.contentList.rows.addAll(res.rows);
    }

    state.contentList.total = res.total;
    state.hasMore =
        state.pagerRequest.pageNum * state.pagerRequest.pageSize < res.total;
    state.pagerRequest.pageNum++;
    update(['contentList']);
  }

  /// 上拉下载
  Future onRichBottom() async {
    try {
      if (state.hasMore) {
        await getList(refresh: false);
      }
      if (!state.hasMore) {
        state.refreshController.finishLoad(IndicatorResult.noMore);
      } else {
        state.refreshController.finishLoad(IndicatorResult.success);
      }
    } catch (e) {
      state.refreshController.finishLoad(IndicatorResult.fail);
    }
  }

  /// 点赞、取消点赞
  void onGiveHandle(bool isGive, int? noteId) {
    if (noteId == null) {
      return LoadingUtil.showError('点赞失败');
    }
    var resourceRequest =
        ResourceRequest(resourceType: ResourceType.content, resourceId: noteId);
    if (isGive) {
      state.contentRepository.giveAdd(resourceRequest);
    } else {
      state.contentRepository.giveCancel(resourceRequest);
    }
  }

  /// 收藏、取消收藏
  void onCollectionHandle(bool isGive, int? noteId) {
    if (noteId == null) {
      return LoadingUtil.showError('收藏失败');
    }
    var resourceRequest =
        ResourceRequest(resourceType: ResourceType.content, resourceId: noteId);
    if (isGive) {
      state.contentRepository.collectionAdd(resourceRequest);
    } else {
      state.contentRepository.collectionCancel(resourceRequest);
    }
    state.needRefresh = true;
  }
}
